#include "stdafx.h"

#include "ListProductTable.h"

CListProductTbl::CListProductTbl()
{
	m_sName = TABLE_LIST_PRODUCT;
}
 bool CListProductTbl::addNewProduct(const CListProduct* pList)
 {
	 CString sQueryAdd;
	 sQueryAdd.Format(_T("( '%s',_utf8'%s','%s','%s',%d,'%s','%s','%s',%d,'%s','%s')")
		 ,pList->getCode(),pList->getName(),pList->getStore(),pList->getGroup(),pList->getCountMin(),
		 pList->getPriceInput(),pList->getPriceSell(),pList->getPriceWhole(),pList->getType(),pList->getCodeProvider(),pList->getBonus());
	 CSqlTable::m_sName = TABLE_LIST_PRODUCT;
	return insertRecord(TBL_PRODUCT,sQueryAdd);
 }
CListProduct* CListProductTbl::getList(CString sCode)
{
	CListProduct* pList = NULL;
	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s where %s ='%s'"),g_sDatabase,TABLE_LIST_PRODUCT,FIELD_CODE_PRODUCT,sCode);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			pList = new CListProduct;
			pList->setCode(CString(row[0]));
			pList->setName(CString (CA2W(row[1], CP_UTF8)));
			pList->setStore(CString(row[2]));
			pList->setGroup(CString(row[3]));
			pList->setCountMin(atoi(row[4]));
			pList->setPriceInput(CString(row[5]));
			pList->setPriceSell(CString(row[6]));
			pList->setPriceWhole(CString(row[7]));
			pList->setType(atoi(row[8]));
			pList->setCodeProvider(CString(row[9]));
			pList->setBonus(CString(row[10]));
		}
		mysql_free_result(pResult);
	}
	return pList;
}

 vector<CListProduct*> CListProductTbl::getListProduct()
 {
	 vector<CListProduct*> vtList;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	sNewQuery.Format(_T("SELECT * FROM %s.%s "),g_sDatabase,TABLE_LIST_PRODUCT);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 CListProduct* pList = new CListProduct;
			 pList->setCode(CString(row[0]));
			 pList->setName(CString (CA2W(row[1], CP_UTF8)));
			 pList->setStore(CString(row[2]));
			 pList->setGroup(CString(row[3]));
			 pList->setCountMin(atoi(row[4]));
			 pList->setPriceInput(CString(row[5]));
			 pList->setPriceSell(CString(row[6]));
			 pList->setPriceWhole(CString(row[7]));
			 pList->setType(atoi(row[8]));
			 pList->setCodeProvider(CString(row[9]));
			 pList->setBonus(CString(row[10]));
			 vtList.push_back(pList);
		 }
		 mysql_free_result(pResult);
	 }
	 return vtList;
 }
 
 bool CListProductTbl::updateSetting(const CListProduct* pList)
 {
	 bool bResult = false;
	 if (pList)
	 {
		 CString sQuery;
		 MYSQL_ROW row = NULL;
		 sQuery.Format(_T("UPDATE %s.%s SET %s = '%s', %s= '%d' WHERE %s ='%s'"),
			 g_sDatabase,TABLE_LIST_PRODUCT,
			 FIELD_BONUS,pList->getBonus(),
			 FIELD_MIN_COUNT,pList->getCountMin(),
			 FIELD_CODE_PRODUCT,pList->getCode()
			 );
		 CT2A ascii(sQuery, CP_UTF8);	
		 if(mysql_query(conn,ascii.m_psz)==0)
			 bResult = true;
	 }
	 return bResult;
 }

 vector<CString> CListProductTbl::getListCode(const CString& sFind)
 {
	 vector<CString> vtList;

	 MYSQL_RES * pResult = NULL;
	 CString sNewQuery,sAdd;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE  %s.%s LIKE '"),g_sDatabase,TABLE_LIST_PRODUCT,TABLE_LIST_PRODUCT,_T("ten_hang"));
	  sNewQuery += _T("%");
	 sAdd.Format(_T("%s"),sFind);
	 sNewQuery += sAdd;
	  sNewQuery += _T("%'");
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = NULL;
		 while((row = mysql_fetch_row(pResult)) )
		 {
			 vtList.push_back(CString(row[0]));
		 }
		 mysql_free_result(pResult);
	 }
	 return vtList;
 }
 int  CListProductTbl::getMaxCode()
 {
	 int nMax = 0;
	 MYSQL_RES * pResult = NULL;

	 CString sNewQuery;
	 mysql_query(conn,"SET character_set_results=utf8");
	 sNewQuery.Format(_T("SELECT %s ,MAX(%s)FROM %s.%s "),FIELD_CODE_PRODUCT,FIELD_CODE_PRODUCT,g_sDatabase,TABLE_LIST_PRODUCT);
	 CT2A ascii(sNewQuery, CP_UTF8);		
	 mysql_query(conn,ascii.m_psz);
	 pResult = mysql_use_result(conn);
	 if (pResult)
	 {
		 MYSQL_ROW row = mysql_fetch_row(pResult);
		 if (row)
		 {
			 CString sTemplate;
			 sTemplate = row[1];
			 sTemplate.Replace(_T("HH"),_T("0"));
			 nMax = _tstoi(sTemplate);
		 }
		mysql_free_result(pResult);
	 }
	 return nMax;
 }
